<template>
  <div class="mobile-register">
    <van-nav-bar 
      title="用户注册" 
      left-text="返回"
      left-arrow
      @click-left="goBack"
    />
    
    <div class="register-container">
      <div class="logo-section">
        <van-image
          width="60"
          height="60"
          src="/logo.png"
          round
        />
        <h2 class="app-title">{{ title }}</h2>
      </div>

      <van-form @submit="handleRegister" class="register-form">
        <!-- 用户名输入框 -->
        <van-field
          v-model="registerForm.username"
          name="username"
          label="用户名"
          placeholder="请输入用户名"
          :rules="[
            { required: true, message: '请输入用户名' },
            { min: 2, max: 20, message: '用户名长度必须在2-20个字符之间' }
          ]"
          left-icon="user-o"
        />
        
        <!-- 密码输入框 -->
        <van-field
          v-model="registerForm.password"
          type="password"
          name="password"
          label="密码"
          placeholder="请输入密码"
          :rules="[
            { required: true, message: '请输入密码' },
            { min: 5, max: 20, message: '密码长度必须在5-20个字符之间' }
          ]"
          left-icon="lock"
        />
        
        <!-- 确认密码输入框 -->
        <van-field
          v-model="registerForm.confirmPassword"
          type="password"
          name="confirmPassword"
          label="确认密码"
          placeholder="请再次输入密码"
          :rules="[
            { required: true, message: '请再次输入密码' },
            { validator: validateConfirmPassword, message: '两次输入的密码不一致' }
          ]"
          left-icon="lock"
        />
        
        <!-- 验证码输入框 -->
        <van-field
          v-if="captchaEnabled"
          v-model="registerForm.code"
          name="code"
          label="验证码"
          placeholder="请输入验证码"
          :rules="[{ required: true, message: '请输入验证码' }]"
          left-icon="shield-o"
        >
          <template #button>
            <van-image
              :src="codeUrl"
              width="80"
              height="32"
              @click="getCode"
              style="cursor: pointer;"
            />
          </template>
        </van-field>
        
        <!-- 注册按钮 -->
        <div class="register-button">
          <van-button
            round
            block
            type="primary"
            native-type="submit"
            :loading="loading"
            size="large"
          >
            {{ loading ? '注册中...' : '注册' }}
          </van-button>
        </div>
        
        <!-- 登录链接 -->
        <div class="login-link">
          <van-button
            type="default"
            size="small"
            plain
            @click="goToLogin"
          >
            已有账号？立即登录
          </van-button>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { showSuccessToast, showFailToast } from 'vant'
import { register, getCodeImg } from '@/api/login'

// 获取应用标题
const title = import.meta.env.VITE_APP_TITLE

// 路由
const router = useRouter()

// 响应式数据
const registerForm = reactive({
  username: '',
  password: '',
  confirmPassword: '',
  code: '',
  uuid: ''
})

const loading = ref(false)
const captchaEnabled = ref(true)
const codeUrl = ref('')

// 确认密码验证
function validateConfirmPassword() {
  return registerForm.password === registerForm.confirmPassword
}

// 注册处理函数
async function handleRegister() {
  try {
    loading.value = true
    
    // 调用注册API
    await register(registerForm)
    
    // 注册成功提示
    showSuccessToast('注册成功！')
    
    // 跳转到登录页面
    setTimeout(() => {
      router.push('/mobile-login')
    }, 1500)
    
  } catch (error) {
    console.error('注册失败:', error)
    showFailToast('注册失败，请检查输入信息')
    
    // 重新获取验证码
    if (captchaEnabled.value) {
      getCode()
    }
  } finally {
    loading.value = false
  }
}

// 获取验证码
async function getCode() {
  try {
    const res = await getCodeImg()
    captchaEnabled.value = res.captchaEnabled === undefined ? true : res.captchaEnabled
    
    if (captchaEnabled.value) {
      codeUrl.value = 'data:image/gif;base64,' + res.img
      registerForm.uuid = res.uuid
    }
  } catch (error) {
    console.error('获取验证码失败:', error)
    showFailToast('获取验证码失败')
  }
}

// 返回上一页
function goBack() {
  router.back()
}

// 跳转到登录页面
function goToLogin() {
  router.push('/mobile-login')
}

// 组件挂载时执行
onMounted(() => {
  getCode()
})
</script>

<style scoped>
.mobile-register {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  flex-direction: column;
}

.register-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 20px;
  padding-top: 20px;
}

.logo-section {
  text-align: center;
  margin-bottom: 30px;
}

.app-title {
  margin-top: 12px;
  color: white;
  font-size: 24px;
  font-weight: 600;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.register-form {
  background: white;
  border-radius: 20px;
  padding: 32px 24px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.15);
  margin-bottom: 20px;
}

.register-button {
  margin: 32px 0 20px 0;
}

.login-link {
  text-align: center;
  margin-top: 20px;
}

/* 自定义 Vant 组件样式 */
:deep(.van-field__label) {
  color: #333;
  font-weight: 500;
  font-size: 16px;
}

:deep(.van-field__control) {
  color: #333;
  font-size: 16px;
}

:deep(.van-field__placeholder) {
  color: #999;
}

:deep(.van-field) {
  background-color: #f8f9fa;
  border-radius: 12px;
  margin-bottom: 16px;
  border: 2px solid transparent;
  transition: all 0.3s ease;
}

:deep(.van-field:focus-within) {
  border-color: #667eea;
  background-color: white;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

:deep(.van-field__left-icon) {
  color: #667eea;
}

:deep(.van-field__control) {
  background-color: transparent;
}

:deep(.van-button--primary) {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  height: 48px;
  font-size: 18px;
  font-weight: 600;
  border-radius: 24px;
}

:deep(.van-button--primary:active) {
  transform: scale(0.98);
}

:deep(.van-nav-bar) {
  background: transparent;
  color: white;
}

:deep(.van-nav-bar__title) {
  color: white;
  font-weight: 600;
}

:deep(.van-nav-bar__text) {
  color: white;
}

/* 响应式设计 */
@media (max-width: 375px) {
  .register-container {
    padding: 16px;
  }
  
  .register-form {
    padding: 24px 20px;
  }
  
  .app-title {
    font-size: 20px;
  }
}
</style>
